package com.briup.jd2511.jd2511_product_source.service.impl;

import cn.hutool.core.util.StrUtil;
import com.briup.jd2511.jd2511_product_source.dao.ManagerHurdlesMapper;
import com.briup.jd2511.jd2511_product_source.dao.ext.ManagerHurdlesExtMapper;
import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerHurdles;
import com.briup.jd2511.jd2511_product_source.pojo.ext.ManagerHurdlesExt;
import com.briup.jd2511.jd2511_product_source.service.HurdlesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HurdlesServiceImpl implements HurdlesService {

    @Autowired
    private ManagerHurdlesMapper managerHurdlesMapper;
    @Autowired
    private ManagerHurdlesExtMapper managerHurdlesExtMapper;

    @Override
    public List<Integer> queryAllMax() throws ServiceException {
        return managerHurdlesMapper.selectAllHMax();
    }

    @Override
    public PageInfo<ManagerHurdlesExt> getAllByConditionByPage(String hName, Integer hMax, String fhName, String hEnable, Integer pageNum, Integer pageSize) throws ServiceException {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(managerHurdlesExtMapper
                .selectAllByConditionByPageWithFenceHose(
                        hName, hMax, fhName, hEnable));
    }

    @Override
    public void saveOrUpdate(ManagerHurdles managerHurdles) throws ServiceException {
        String hId = managerHurdles.getHId();
        if(StrUtil.isEmpty(hId)){
            // 新增
        }else {
            // 修改
            managerHurdlesMapper.updateSelective(managerHurdles);
        }
    }

    @Override
    public void updateHEnable(String hId, String hEnable) throws ServiceException {
        ManagerHurdles managerHurdles = new ManagerHurdles();
        managerHurdles.setHId(hId);
        String temp = "";
        if("可用".equals(hEnable)){
            temp="禁用";
        }else if ("禁用".equals(hEnable)){
            temp="可用";
        }
        managerHurdles.setHEnable(temp);
        saveOrUpdate(managerHurdles);
    }

    @Override
    public void updateHEnableBatch(List<Map<String,String>> list) throws ServiceException {
        // 遍历list集合完成
        list.forEach(map->{
            updateHEnable(map.get("hId"),map.get("hEnable"));
        });
    }

    @Override
    public List<ManagerHurdles> queryAllEnable() throws ServiceException {
        return managerHurdlesMapper.selectAllEnable();
    }

}
